package com.software.messagecenter.service;

import com.software.messagecenter.entity.Message;
import com.software.messagecenter.entity.Student;
import com.software.messagecenter.entity.Teacher;
import com.software.messagecenter.entity.User;
import com.software.messagecenter.feignclient.UserCenterFeignClient;
import com.software.messagecenter.repository.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MessageService {
    @Autowired
    private UserCenterFeignClient userCenterFeignClient;

    @Autowired
    private MessageRepository messageRepository;

    public List<User> findReceiverBySendId(String Id) {

        List<Message> messageList = messageRepository.findMessagesByUserId(Id);
        List<String> collectReceive = messageList.stream().flatMap(o -> Stream.of(o.getReceiverId(),o.getSenderId())).filter(s -> !s.equals(Id)).distinct().collect(Collectors.toList());
//        List<String> collectReceive = messageList.stream().sorted(Comparator.comparing(Message::getSendTime)).flatMap(o -> Stream.of(o.getReceiverId(),o.getSenderId())).filter(s -> !s.equals(Id)).distinct().collect(Collectors.toList());

        List<User> list = new ArrayList<>();


        for (int i = 0; i<collectReceive.size();i++){
            User user = new User();
            String s = collectReceive.get(i);
            Student student = userCenterFeignClient.findStudentInfoByUserId(s);
            if (student != null){
                user.setStudent(student);
                user.setTeacher(null);
                list.add(user);
                System.out.println(user);
            }else {
                Teacher teacher = userCenterFeignClient.findTeacherInfoByUserId(s);
                user.setTeacher(teacher);
                user.setStudent(null);
                list.add(user);
                System.out.println(user);
            }
        }


        return list;
    }

    public List<Message> findMessagesBySenderIdAndReceiverId(String id1, String id2) {
        List<Message> messagesBySenderIdAndReceiverId = messageRepository.findMessagesBySenderIdAndReceiverId(id1, id2);
        List<Message> messagesBySenderIdAndReceiverId2 = messageRepository.findMessagesBySenderIdAndReceiverId(id2, id1);
        messagesBySenderIdAndReceiverId.addAll(messagesBySenderIdAndReceiverId2);
//        List<Message> list = messagesBySenderIdAndReceiverId.stream().sorted(Comparator.comparing(Message::getSendTime).reversed()).collect(Collectors.toList()); 倒序排列数据
        List<Message> list = messagesBySenderIdAndReceiverId.stream().sorted(Comparator.comparing(Message::getSendTime)).collect(Collectors.toList());
        return list;
    }

    public Message addClassMessage(Message create) {
        Message save = messageRepository.save(create);
        return save;
    }
}
